﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using BDS;
using Utils.DCSControl;
using Utils.DatabaseUtils;
using Utils.Entity;
using Telerik.Web.UI;
using DCTLib.Common;
using DCTAccess.Business.Search;
using DCTAccess.Business.Property;
using DCTAccess.Business.Member;

public partial class TimKiem_tim_kiem_nang_cao : System.Web.UI.Page
{
    BDS.ProductUI objProduct = new BDS.ProductUI();
    FunctionPageUI objCategoryPage = new FunctionPageUI();
    LocationUI objLocation = new LocationUI();
    DataSet dsLocation;
    NeedsUI objNeeds = new NeedsUI();
    DataSet dsNeeds;
    DataSet dsCategoryPage;
    private string strKeyword = string.Empty;
    private string strServices = string.Empty;
    private int strFromPrice = -1;
    private int strToPrice = -1;
    private int strLocation = -1;
    private string strNeed = string.Empty;

    public string Message
    {
        get { return lblError.Text; }
        set { lblError.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetAll();
        dsLocation = objLocation.GetAll();
        dsNeeds = objNeeds.GetAll();
        if (!IsPostBack)
        {
            try
            {
                BindCategoryPage();
                BindLocation();
                //BindDataGrid(0);
            }
            catch (System.Exception ex)
            {
                return;
            }
        }
        Message = "";
    }

    private void BindCategoryPage()
    {
        try
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Value");

            DataRow newRow = null;

            int newsId = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["NewsID"]);

            DataRow[] rows = dsCategoryPage.Tables[0].Select("ParentId = " + newsId);
            foreach (DataRow row in rows)
            {
                newRow = dt.NewRow();
                newRow["ID"] = row["FunctionPageAlias"].ToString().Trim() + "/tat-ca-chung-loai";
                newRow["Value"] = row["FunctionPageName"].ToString().Trim();
                dt.Rows.Add(newRow);

                DataRow[] childRows = dsCategoryPage.Tables[0].Select("ParentId = " + row["FunctionPageID"]);
                foreach (DataRow childRow in childRows)
                {
                    newRow = dt.NewRow();
                    newRow["ID"] = row["FunctionPageAlias"].ToString().Trim() + "/" + childRow["FunctionPageAlias"].ToString().Trim();
                    newRow["Value"] = childRow["FunctionPageName"].ToString().Trim();
                    dt.Rows.Add(newRow);
                }
            }

            cboCategory.DataSource = dt;
            cboCategory.DataBind();
        }
        catch (Exception e)
        {

        }
    }
    private void BindLocation()
    {
        try
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Value");

            DataRow newRow = null;

            DataRow[] rows = dsLocation.Tables[0].Select("ParentId is null");
            foreach (DataRow row in rows)
            {
                newRow = dt.NewRow();
                newRow["ID"] = row["LocationAlias"].ToString().Trim() + "/tat-ca-quan-huyen";
                newRow["Value"] = row["LocationName"].ToString().Trim();
                dt.Rows.Add(newRow);

                DataRow[] childRows = dsLocation.Tables[0].Select("ParentId = " + row["LocationID"]);
                foreach (DataRow childRow in childRows)
                {
                    newRow = dt.NewRow();
                   // newRow["ID"] = row["LocationAlias"].ToString().Trim() + "/" + childRow["LocationAlias"].ToString().Trim();
                    newRow["ID"] = row["LocationID"].ToString().Trim();
                    newRow["Value"] = childRow["LocationName"].ToString().Trim();
                    dt.Rows.Add(newRow);
                }
            }

            cboLocation.DataSource = dt;
            cboLocation.DataBind();
        }
        catch (Exception e)
        {

        }
    }
    protected void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            //int cateId = Convert.ToInt32(((DropDownList)sender).SelectedValue);
            string alias = ((DropDownList)sender).SelectedValue.Trim();

            string parentCategory = alias.Substring(0, alias.IndexOf("/"));
            cboNhuCau.DataSource = TableLib.CreateTable(dsNeeds.Tables[0], "CategoryPageID = " + (Convert.ToInt32(dsCategoryPage.Tables[0].Select("FunctionPageAlias = '" + parentCategory + "'")[0]["FunctionPageID"])));
            cboNhuCau.DataBind();
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (CheckDataInput())
        {
            Message = string.Empty;
            strKeyword = txtKeyword.Text.Trim();
            //strServices = cboCategory.SelectedValue.ToString();
            if (cboCategory.SelectedIndex == 0)
                strServices = "-1";
            else
                strServices = cboCategory.SelectedItem.ToString();// +"[]" + cboCategory.SelectedValue.ToString();


            if (cboNhuCau.SelectedIndex == 0)
                strNeed = "-1";
            else
                strNeed = cboNhuCau.SelectedItem.ToString();// +"[]" + cboNhuCau.SelectedIndex.ToString();

            if (cboLocation.SelectedIndex == 0)
                strLocation = -1;
            else
                strLocation = Convert.ToInt32(cboLocation.SelectedValue.ToString()); //cboLocation.SelectedItem.ToString() + "[]" + 



            if (txtFromCurrentPrice.Text == "")
            {
                strFromPrice = -1;
            }
            else
            {
                strFromPrice = Convert.ToInt32(txtFromCurrentPrice.Text);
            }

            if (txtToCurrentPrice.Text == "")
            {
                strToPrice = -1;
            }
            else
            {
                strToPrice = Convert.ToInt32(txtToCurrentPrice.Text);
            }

            //strLocation = cboLocation.SelectedItem.ToString();
            //strNeed = cboNhuCau.SelectedItem.ToString();           

            Message += "Tu khoa: " + strKeyword + "<br/>";
            Message += "Tu service: " + strServices + "<br/>";
            Message += "Tu gia bd: " + strFromPrice + "<br/>";
            Message += "Tu gia sau: " + strToPrice + "<br/>";
            Message += "Tu location: " + strLocation + "<br/>";
            Message += "Tu need: " + strNeed + "<br/>";

            try
            {
                SearchFilter filter = GetSearchDetail();
                DataTable dataTable = filter.GetSearchResult();
                if (!ValidationHelper.HasRow(dataTable))
                {
                    // DatatableHelper.HasNoRow(dataTable, "ProductName", gvSearch);
                    //Session["SearchAvance"] = dataTable;
                }
                else
                {
                    //ControlLoader.BindDatagrid(gvSearch, filter.GetSearchResult(), 0);
                    Session["SearchAvance"] = dataTable;
                }
            }
            catch (System.Exception ex)
            {
                //Message = "error";
            }
            Server.Transfer("ket-qua-tim-kiem.aspx");

            //            Server.Transfer(string.Format(@"ket-qua-tim-kiem.aspx
            //                ?Keyword={0}&Services={1}&FromPrice={2}&ToPrice={3}&Location={4}&Need={5}"
            //                , strKeyword, strServices, strFromPrice, strToPrice, strLocation, strNeed));

        }
    }

    //private void BindDataGrid(int pageIndex)
    //{
    //    SearchFilter filter = GetSearchDetail();
    //    ControlLoader.BindDatagrid(gvSearch, filter.GetSearchResult(), pageIndex);
    //    DataTable dataTable = filter.GetSearchResult();
    //    Message = dataTable.Rows.Count + " result(s) found";
    //}

    private SearchFilter GetSearchDetail()
    {
        SearchFilter filter = new SearchFilter();
        filter.Title = strKeyword;
        filter.Services = strServices;
        filter.Price = strFromPrice;
        filter.PriceTo = strToPrice;
        filter.LocationID = strLocation;
        filter.Need = strNeed;
        return filter;
    }

    private bool CheckDataInput()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtKeyword.Text.Trim()))
        {
            Message += "Bạn phải nhập từ khóa tìm kiếm " + "<br>";
            validated = false;
        }

        if (txtFromCurrentPrice.Text == "")
        {
            strFromPrice = -1;
        }
        else
        {
            strFromPrice = Convert.ToInt32(txtFromCurrentPrice.Text);
        }

        if (txtToCurrentPrice.Text == "")
        {
            strToPrice = -1;
        }
        else
        {
            strToPrice = Convert.ToInt32(txtToCurrentPrice.Text);
        }

        return validated;
    }

    private void ClearInput()
    {

    }

    public string FormatPrice(object num)
    {
        if (num.ToString() == "-1")
            return "Thỏa thuận";
        return String.Format("{0:#,##}", num);
    }

    //protected void gvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{

    //}
}
